using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SkillChoosePanel : BasePanel
{
    static readonly string path = "UI/Panel/SkillChoosePanel";
    static readonly string skillpath = "UI/Skill/skill";
    static readonly string panelpath = "UI/SkillinPanel/";
    static readonly string playerpath = "UI/SkillinPlayer/";
    public SkillChoosePanel() : base(new UIType(path)) { }
    private GameObject[] keycodepicture=new GameObject[4];
    private GameObject[] button = new GameObject[4];
    //private GameObject[] skillinpanel=new GameObject[4];
    //private GameObject[] skillinplayer=new GameObject[4];
    //string skillnames = new string[4];
    //string extranames = new string[4];
    //MonoBehaviour thispanel;
    int num = 0;
    
    public override void OnEnter()
    {
        keycodepicture[0] = Resources.Load<GameObject>("UI/Skill/skillU");
        keycodepicture[1] = Resources.Load<GameObject>("UI/Skill/skillI");
        keycodepicture[2] = Resources.Load<GameObject>("UI/Skill/skillO");
        keycodepicture[3] = Resources.Load<GameObject>("UI/Skill/skillP");
        for (int i = 0; i < 4; i++)
            PlayerData.extranames[i] = "000";

        uitool.GetOrAddComponentInChildren<Button>("wingyong").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "wingyong";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num]= Resources.Load<GameObject>(panelpath+skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("sprinting").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "sprinting";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num] = Resources.Load<GameObject>(panelpath + skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("tornado").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "tornado";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num] = Resources.Load<GameObject>(panelpath + skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("shield").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "shield";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num] = Resources.Load<GameObject>(panelpath + skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("heal").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "heal";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num] = Resources.Load<GameObject>(panelpath + skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("bloodrage").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "bloodrage";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num] = Resources.Load<GameObject>(panelpath + skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("remake").onClick.AddListener(() =>
        {
            for(int i = 0; i < num; i++)
            {

                button[i].GetComponent<CanvasGroup>().interactable = true;
            }
            GameObject[] targets = GameObject.FindGameObjectsWithTag("skill");
            foreach (GameObject target in targets)
            {
                  GameObject.Destroy(target);
            }
            num = 0;
        });
        uitool.GetOrAddComponentInChildren<Button>("civilization").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "civilization";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num] = Resources.Load<GameObject>(panelpath + skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("barbette").onClick.AddListener(() =>
        {
            if (num < 4)
            {
                string skillname = "barbette";
                PlayerData.skillnames[num] = skillname;
                PlayerData.skillinpanel[num] = Resources.Load<GameObject>(panelpath + skillname);
                PlayerData.skillinplayer[num] = Resources.Load<GameObject>(playerpath + skillname + "000");
                button[num] = GameObject.Find(skillname);
                button[num].GetComponent<CanvasGroup>().interactable = false;
                GameObject.Instantiate(keycodepicture[num], button[num].transform);
                num++;
            }
        });
        uitool.GetOrAddComponentInChildren<Button>("sure").onClick.AddListener(() =>
        {
            if (num == 4)
            {
                 GameRoot.Instance.scenesystem.SetScene(new MapScene());
            }
        });
    }
}
